package com.example.helllo;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class RateTask implements Runnable{
    public static final String TAG = "RateTask";
    Handler handler;
    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {
        Log.i(TAG,"run:......");
        List<Rate> ret = new ArrayList<Rate>();
        //ArrayList<HashMap<String,String>> ret = new ArrayList<HashMap<String,String>>();

        //Thread.sleep(3000);
        Document doc = null;
        try {
            doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/index.html").get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(TAG,"title:"+doc.title());
        Element table = doc.getElementsByTag("table").get(1);
        Elements trs = table.getElementsByTag("tr");
        for(Element tr:trs){
            Elements tds = tr.getElementsByTag("td");
            if(tds.size()>0){
                String str = tds.get(0).text();
                String val = tds.get(5).text();
                Log.i(TAG,"run: td1="+str+"-->"+val);
                //ret.add(str+"-->"+val);
                ret.add(new Rate(str,val));
            }
        }

        //返回消息到主线程
        Message msg = handler.obtainMessage(11,ret);
        handler.sendMessage(msg);
    }

}

